<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <fieldset>
            <legend>Thêm sản phẩm</legend>
            <form action="add_product.php" method="post" enctype="multipart/form-data">
                name:<input type="text" name="pro1name"/><br/>
                description:<input cols="10" rows="5" name="pro1desc"/><br/>
                price:<input type="text" name="pro1price"/><br/>
                thump:<input type="file" name="pro1thump"/><br/>
                images:<input type="file" name="img[]" multiple=""/><br/>
                category:<input type="text" name="pro1cat"/><br/>
                <input type="submit" name="btnSubmit" value="Upload"/>
            </form>
        </fieldset>
        <a href="index.php">quay lại index</a>
        <?php

        function __autoload($cls) {
            require $cls . '.php';
        }

        if (isset($_POST['btnSubmit'])) {
            $n = $_POST['pro1name'];
            $d = $_POST['pro1desc'];
            $p = $_POST['pro1price'];
            $c = $_POST['pro1cat'];
            $files = $_FILES['pro1thump'];
            $error = $_FILES['pro1thump']['error'];
            $file_name = $_FILES['pro1thump']['name'];
            $tmp_name = $_FILES['pro1thump']['tmp_name'];
            $type = $_FILES['pro1thump']['type'];
            $mul_files = $_FILES['img'];
            $mul_error = $_FILES['img']['error'];
            $mul_name = $_FILES['img']['name'];
            $mul_tmp = $_FILES['img']['tmp_name'];
            $mul_type = $_FILES['img']['type'];

            
            $pro = new product();
            
            $data1 = array("name" => "$n", "des" => "$d", "price" => "$p", "price1" => "$p", "cat_id" => "$c", "image" => "images/products/$file_name");
            if ($error == 0) {
                if (!file_exists('images/' . $file_name)) {
                    move_uploaded_file($tmp_name, '../images/products/' . $file_name);
                } else {
                    echo "this file exists!";
                }
            }
            $pro->addProduct($data1);
            $last_id = $pro->db->_id;
            $img = new images();

            for ($i = 0; $i < count($mul_name); $i++) {
            if ($mul_error[$i] == 0) {
                if (!file_exists('images/' . $mul_name[$i])) {
                    move_uploaded_file($mul_tmp[$i], '../images/products/' . $mul_name[$i]);
                    $img = new images();
                    $data2 = array("pro_id" => "$last_id", "url" => "images/products/$mul_name[$i]");
                    $img->addImages($data2);
                    $img->db->close();
                } else {
                    echo "this file exists!";
                }
            }
        }
            
        }
        ?>
    </body>
</html>
